#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.1
#####

library(here)
library(data.table)
library(xtable)

# load the pooled data if not already in workspace
if(!exists("pooled")){
  pooled <- fread(here("data", "pooled.csv"))
}

# load the behavioral data if not already in workspace
if(!exists("behav")){
  behav <- fread(here("data", "behav.csv"))
}

# Objects holding printing and code names of variables --------------------

outcomes_main <- c(
  "Comprehension Item 1" = "attention_1",
  "Comprehension Item 2" = "attention_2",
  "Comprehension Item 3" = "attention_3",
  "Norms Statement" = "mechanism_NM",
  "Countering Stereotypes Statement" = "mechanism_CS",
  "Common Humanity Statement" = "mechanism_CH",
  "Immigrant Neighbors" = "imm_neighbors2",
  "Increase Immigration" = "imm_increase2",
  "Immigration Index" = "immPCA",
  "Imm. Index (Alt. Const.)" = "immAVG",
  "Second Order Beliefs" = "second_order_beliefs2",
  "Discomfort Criticizing Immigrants" = "prejudice_discomfort2"
)

treatments_main <- c(
  "Treatment: Norms" = "NM",
  "Treatment: Countering Stereotypes" = "CS",
  "Treatment: Common Humanity" = "CH",
  "Treatment: Sleep (control)" = "Treatment == \"Sleep\""
)

covariates_main <- c(
  "Age: 18-29" = "age %in% 18:29",
  "Age: 30-39" = "age %in% 30:39",
  "Age: 40-49" = "age %in% 40:49",
  "Age: 50-59" = "age %in% 50:59",
  "Age: 60+" = "age >= 60",
  "Female" = "female",
  "Race: White/Caucasian" = "race2 == \"White/Caucasian\"",
  "Race: Asian" = "race2 == \"Asian\"",
  "Race: African American/Black" = "race2 == \"Black/African American\"",
  "Race: Hispanic/Latino" = "race2 == \"Hispanic/Latino\"",
  "Race: Other" = "race2 == \"Other (please specify)\" | is.na(race2)",
  "Education: Less than high school" = "edu2 == \"Less than high school\"",
  "Education: High school graduate/GED" = "edu2 == \"High school graduate/ GED\"",
  "Education: 2 year degree" = "edu2 == \"2 year degree\"",
  "Education: Some college" = "edu2 == \"Some college\"",
  "Education: 4 year degree" = "edu2 == \"4 year degree\"",
  "Education: Masters degree" = "edu2 == \"Masters degree\"",
  "Education: Professional Degree (JD, MD)" = "edu2 == \"Professional Degree (JD, MD)\"",
  "Education: Doctorate" = "edu2 == \"Doctorate\"",
  "Party ID: Democrat" = "pid2 == \"Democrat\"",
  "Party ID: Republican" = "pid2 == \"Republican\"",
  "Party ID: Independent" = "pid2 == \"Independent\"",
  "Party ID: Something else" = "pid2 == \"Something else\"",
  "News Attention" = "news2",
  "Region: Northeast" = "region2 == \"Northeast\"",
  "Region: Midwest" = "region2 == \"Midwest\"",
  "Region: South" = "region2 == \"South\"",
  "Region: West" = "region2 == \"West\"",
  "Survey Wave: 1" = "wave == 1",
  "Survey Wave: 2" = "wave == 2",
  "Survey Wave: 3" = "wave == 3"
)


# Calculations and prepare nice plotting (individual level) ---------------

descriptives_main <-
  data.frame(
    t(
      pooled[replication == 0 , lapply(
        c(outcomes_main, treatments_main, covariates_main),
        function(x){
          c("min" = min(eval(parse(text = x)), na.rm = T),
            "mean" = mean(eval(parse(text = x)), na.rm = T),
            "max" = max(eval(parse(text = x)), na.rm = T),
            "sd" = sd(eval(parse(text = x)), na.rm = T),
            "N" = length(na.omit(eval(parse(text = x)))))
        }
      )]
    )
  )

colnames(descriptives_main) <- c("Min","Mean", "Max", "Std. Dev.", "N")

descriptives_main$N <- prettyNum(descriptives_main$N, big.mark = ",")


# Calculations and prep for behavioral game variables ---------------------

vars_game <- c(
  "Tokens Given (all rounds)" = "tokens",
  "Tokens Given, Round 1" = "tokens[game_NR == 1]",
  "Tokens Given, Round 2" = "tokens[game_NR == 2]",
  "Tokens Given, Round 3" = "tokens[game_NR == 3]",
  "Tokens Given, Round 4" = "tokens[game_NR == 4]",
  "Female Partner" = "female_partner",
  "Foreign-Born Partner" = "foreign_partner"
)

descriptives_game <-
  data.frame(
    t(
      behav[ , lapply(
        vars_game,
        function(x){
          c("min" = min(eval(parse(text = x)), na.rm = T),
            "mean" = mean(eval(parse(text = x)), na.rm = T),
            "max" = max(eval(parse(text = x)), na.rm = T),
            "sd" = sd(eval(parse(text = x)), na.rm = T),
            "N" = length(na.omit(eval(parse(text = x)))))
        }
      )]
    )
  )

colnames(descriptives_game) <- c("Min","Mean", "Max", "Std. Dev.", "N")
descriptives_game$N <- prettyNum(descriptives_game$N, big.mark = ",")


# Combine into latex output -----------------------------------------------

xtable(
  rbind(descriptives_main, descriptives_game),
  digits = c(0,0,3,0,3,0),
  label = "tab:descriptives",
  caption = "Descriptive Statistics",
  align = c("l", "c","c","c","c","c")
)

